package com.yileiqian.alg;

public class Encoder {
	public static String encode(String text, String key) {
		char[] out = new char[text.length()];
		char[] keys = key.toCharArray();

		for (int i = 0; i < out.length; i++) {
			out[i] = (char) ((text.charAt(i) + keys[i % keys.length]) % 256);
		}

		return new String(out);
	}

	public static String decode(String text, String key) {
		char[] out = new char[text.length()];
		char[] keys = key.toCharArray();

		for (int i = 0; i < out.length; i++) {
			out[i] = (char) ((256 + text.charAt(i) - keys[i % keys.length]) % 256);
		}

		return new String(out);
	}

	public static void main(String args[]) {
		String out = encode("11111111", "012");
		System.out.println(out);
		System.out.println(decode(out, "0"));
	}
}
